package com.blm.controller.merchant;

import com.blm.common.Result;
import com.blm.common.constants.ExceptionConstant;
import com.blm.dto.FoodCategoryDTO;
import com.blm.exception.CommonException;
import com.blm.service.FoodCategoryService;
import com.blm.service.StoreService;
import com.blm.util.SecurityUtil;
import com.blm.vo.FoodCategoryVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "商家API-店铺商品分类管理")
@RestController
@RequestMapping("/api/merchant/stores/{storeId}/categories")
@SecurityRequirement(name = "bearerAuth")
public class MerchantFoodCategoryController {

    @Autowired
    private FoodCategoryService foodCategoryService;
    
    @Autowired
    private StoreService storeService;
    
    @Autowired
    private SecurityUtil securityUtil;
    
    @Operation(summary = "获取商品分类列表", description = "根据店铺id查询其有哪些商品分类")
    @ApiResponse(responseCode = "200", description = "成功", 
            content = @Content(mediaType = "application/json", 
            schema = @Schema(implementation = FoodCategoryVO.class)))
    @GetMapping
    public Result<List<FoodCategoryVO>> getCategories(
            @Parameter(description = "店铺ID", required = true) @PathVariable Long storeId) {
        
        // 校验店铺所有权
        verifyStoreOwnership(storeId);
        
        List<FoodCategoryVO> categories = foodCategoryService.getCategoriesByStoreId(storeId);
        return Result.success(categories);
    }
    
    @Operation(summary = "添加商品分类", description = "为指定店铺添加新的商品分类")
    @ApiResponse(responseCode = "200", description = "添加成功", 
            content = @Content(mediaType = "application/json", 
            schema = @Schema(implementation = FoodCategoryVO.class)))
    @PostMapping
    public Result<FoodCategoryVO> addCategory(
            @Parameter(description = "店铺ID", required = true) @PathVariable Long storeId,
            @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "商品分类信息", required = true,
                    content = @Content(schema = @Schema(implementation = FoodCategoryDTO.class)))
            @Valid @RequestBody FoodCategoryDTO dto) {
        
        // 校验店铺所有权
        verifyStoreOwnership(storeId);
        
        FoodCategoryVO category = foodCategoryService.addCategory(storeId, dto);
        return Result.success(category);
    }
    
    @Operation(summary = "更新商品分类", description = "更新指定店铺的商品分类信息")
    @ApiResponse(responseCode = "200", description = "更新成功", 
            content = @Content(mediaType = "application/json", 
            schema = @Schema(implementation = FoodCategoryVO.class)))
    @PutMapping("/{id}")
    public Result<FoodCategoryVO> updateCategory(
            @Parameter(description = "店铺ID", required = true) @PathVariable Long storeId,
            @Parameter(description = "分类ID", required = true) @PathVariable Long id,
            @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "商品分类信息", required = true,
                    content = @Content(schema = @Schema(implementation = FoodCategoryDTO.class)))
            @Valid @RequestBody FoodCategoryDTO dto) {
        
        // 校验店铺所有权
        verifyStoreOwnership(storeId);
        
        FoodCategoryVO category = foodCategoryService.updateCategory(storeId, id, dto);
        return Result.success(category);
    }
    
    @Operation(summary = "删除商品分类", description = "删除指定店铺的商品分类")
    @ApiResponse(responseCode = "200", description = "删除成功")
    @DeleteMapping("/{id}")
    public Result<Void> deleteCategory(
            @Parameter(description = "店铺ID", required = true) @PathVariable Long storeId,
            @Parameter(description = "分类ID", required = true) @PathVariable Long id) {
        
        // 校验店铺所有权
        verifyStoreOwnership(storeId);
        
        foodCategoryService.deleteCategory(storeId, id);
        return Result.success(null);
    }
    
    /**
     * 验证当前用户是否是店铺的所有者
     *
     * @param storeId 店铺ID
     */
    private void verifyStoreOwnership(Long storeId) {
        Long userId = securityUtil.getCurrentUserId();
        Long storeOwnerId = storeService.getStoreOwnerId(storeId);
        if (!storeOwnerId.equals(userId)) {
            throw new CommonException(ExceptionConstant.STORE_UNAUTHORIZED);
        }
    }
}
